// const API_URL = 'https://api.thmxp.com' + '/frontend'
// const API_URL = 'https://api.stamp.jizui.top' + '/frontend'
const API_URL = 'https://api.lailiaosheji.com/'
const app = getApp();

//GET请求  
function GET(url, params, requestHandler) {
  request('GET', url, params, requestHandler)
}
//POST请求  
function POST(url, params, requestHandler) {
  request('POST', url, params, requestHandler)
}
//PUT请求  
function PUT(url, params, requestHandler) {
  request('PUT', url, params, requestHandler)
}
//DELETE请求  
function DELETE(url, params, requestHandler) {
  request('DELETE', url, params, requestHandler)
}
/*
请求方法封装与拦截
  method:请求方式，一般为GET或POST
  url:请求路径，一般为去掉提取出来的域名剩下的路径
  requestHandler:请求参数后的处理方法
*/
function request(method, url, params, requestHandler) {
  //注意：可以对params加密等处理  
  const that = this;
  const token = wx.getStorageSync('token');
  wx.request({
    url: API_URL + url,
    data: {
      ...params,
      session_id: token
    },
    method: method, // OPTIONS, GET, HEAD, POST, PUT, DELETE, TRACE, CONNECT  
    header: {
      'Content-Type': 'application/json'
    },
    success: function (res) {
      //注意：可以对参数解密等处理
      if (res.statusCode == 200) {
        if (res.data.code == 10000) {
          wx.removeStorageSync('token');
          wx.getSetting({
            success: res => {
              console.log(res);
              // if (res.authSetting['scope.userInfo']) {
              //   login()
              // }else{
                wx.redirectTo({
                  url: '../index/index',
                })
              // }
            }
          })
        } else if (res.data.code == 500) {
          wx.hideLoading()
          wx.showModal({
            title: '提示',
            content: res.data.msg,
            showCancel: false,
            confirmColor: '#C7A77E',
            success(res) {

            }
          })
        } else {
          requestHandler(res.data);
        }
      }
    },
    fail(res) {
      console.log(res)
      wx.showModal({
        title: '提示',
        content: '网络错误',
        showCancel: false,
        confirmColor: '#C7A77E',
        success(res) {

        }
      })
    },
    complete() {
      // setTimeout(() => {
      //   wx.hideLoading();
      // }, 1000)
    }
  })
}

// // 上传图片
// function uploadImg(url, file, requestHandler) {
//   console.log(file);
//   wx.uploadFile({
//     url: API_URL + url,
//     filePath: file,
//     name: 'file',
//     header: { token: wx.getStorageSync('token') },
//     formData: {},
//     success: function (res) {
//       // console.log(res);
//       requestHandler(res.data)
//     }
//   })
// }

function login(callback, callbackParams) {
  wx.login({
    success: function (res) {
      if (res.code) {
        let code = res.code
        wx.request({
          url: API_URL + '/customer/login',
          data: {
            code: code
          },
          method: 'POST',
          header: {
            'content-type': 'application/json'
          },
          success(res) {
            wx.setStorageSync('token', res.data.data.session_id);
            var page = getCurrentPages().pop();
            if (page == undefined || page == null) return;
            page.onLoad();
            // callback(...callbackParams);
          }
        })
      }
    }
  })
}


module.exports = {
  GET,
  POST,
  PUT,
  DELETE,
  login,
  API_URL
}